package com.sky.controller.admin;

import com.sky.controller.BaseController;
import com.sky.dto.CategoryDTO;
import com.sky.entity.CategoryEntity;
import com.sky.enumeration.CategoryStatusEnum;
import com.sky.enumeration.CategoryTypeEnum;
import com.sky.result.Result;
import com.sky.result.TableDataInfo;
import com.sky.service.CategoryService;
import com.sky.utils.TransformUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类模块")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public TableDataInfo page(Integer page, Integer size, @RequestParam(value = "name",required = false) String name,@RequestParam(value = "type",required = false) String type){
        CategoryTypeEnum typeEnum = TransformUtils.transformEnum(type, CategoryTypeEnum.class);
        log.info("员工分页查询信息：{},{},{},{}",page,size,name,typeEnum);
       List<CategoryEntity> list =  categoryService.page(page,size,name,typeEnum);
        return getTableInfo(list);
    }

    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result del(Long id){
        categoryService.del(id);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public TableDataInfo list(String type){
        CategoryTypeEnum typeEnum = TransformUtils.transformEnum(type, CategoryTypeEnum.class);
       List<CategoryEntity> list =  categoryService.getByType(typeEnum);
       return getTableInfo(list);
    }

    @PostMapping
    @ApiOperation("新增分类")
    public Result add(@RequestBody CategoryDTO dto){
        log.info("新增分类信息，{}",dto);
        categoryService.save(dto);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改分类")
    public Result update(@RequestBody CategoryDTO dto){
        log.info("修改分类信息，{}",dto);
        categoryService.update(dto);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用分类")
    public Result enableOrDisable(@PathVariable String status,Long id){
        CategoryStatusEnum statusEnum = TransformUtils.transformEnum(status, CategoryStatusEnum.class);
        categoryService.enableOrDisable(statusEnum,id);
        return Result.success();
    }

}
